package service

import (
	"gorm.io/gorm"
	"seat-service/initialization"
	"seat-service/model"
	"seat-service/model/dto"
	"seat-service/utils/global"
)

type DictionaryService struct {
}

func (*DictionaryService) AddDictionaryMaster(dictionary model.Dictionary) error {
	err := initialization.DB.Create(&dictionary).Error
	if err != nil {
		return err
	}
	global.AddDictionaryMaster(dictionary)
	return nil
}

func (*DictionaryService) DeleteDictionaryMaster(masterId uint) error {
	err := initialization.DB.Transaction(func(tx *gorm.DB) error {
		err := initialization.DB.Where("dictionary_id=?", masterId).Delete(&model.DictionarySlave{}).Error
		if err != nil {
			return err
		}
		err = initialization.DB.Where("id=?", masterId).Delete(&model.Dictionary{}).Error
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	global.DeleteDictionaryMaster(masterId)
	return nil
}
func (*DictionaryService) BatchDeleteDictionaryMaster(masterId ...uint) error {
	for _, v := range masterId {
		err := initialization.DB.Transaction(func(tx *gorm.DB) error {
			var err error
			slave := make([]model.DictionarySlave, 0)
			err = tx.Model(&model.DictionarySlave{}).Where("dictionary_id=?", v).Find(&slave).Error
			if err != nil {
				return err
			}
			for _, v2 := range slave {
				err = tx.Delete(&model.DictionarySlave{}, v2.ID).Error
				global.DeleteDictionarySlave(v, v2.ID)
				if err != nil {
					return err
				}
			}
			err = tx.Delete(&model.Dictionary{}, v).Error
			if err != nil {
				return err
			}
			return nil
		})
		if err != nil {
			return err
		}
		global.DeleteDictionaryMaster(v)
	}
	return nil
}
func (*DictionaryService) UpdateDictionaryMaster(dictionary model.Dictionary) error {
	err := initialization.DB.Updates(&dictionary).Error
	if err != nil {
		return err
	}
	global.UpdateDictionaryMaster(dictionary)
	return nil
}

func (*DictionaryService) QueryDictionaryMaster(page, pageSize int) (dto.QueryReturnDto, error) {

	dic := make([]model.Dictionary, 0)
	//总数
	var record int64
	err := initialization.DB.Model(&model.Dictionary{}).Count(&record).Error
	if err != nil {
		return dto.QueryReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)

	err = initialization.DB.Model(&model.Dictionary{}).Offset((page) - 1*pageSize).Limit(pageSize).Find(&dic).Error
	if err != nil {
		return dto.QueryReturnDto{}, err
	}

	return dto.QueryReturnDto{
		Record: record,
		Pages:  pages,
		Struct: dic,
	}, nil
}

func (*DictionaryService) QueryDictionaryMasterByType(condition string, page, pageSize int) (dto.QueryReturnDto, error) {
	dic := make([]model.Dictionary, 0)
	//总数
	var record int64
	err := initialization.DB.Model(&model.Dictionary{}).Where("name like ? OR code like ?", "%"+condition+"%", "%"+condition+"%").Count(&record).Error
	if err != nil {
		return dto.QueryReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)

	err = initialization.DB.Model(&model.Dictionary{}).Where("name like ? OR code like ?", "%"+condition+"%", "%"+condition+"%").Offset((page) - 1*pageSize).Limit(pageSize).Find(&dic).Error
	if err != nil {
		return dto.QueryReturnDto{}, err
	}
	return dto.QueryReturnDto{
		Record: record,
		Pages:  pages,
		Struct: dic,
	}, nil
}

func (*DictionaryService) AddDictionarySlave(slave model.DictionarySlave) error {
	err := initialization.DB.Create(&slave).Error
	if err != nil {
		return err
	}
	global.AddDictionarySlave(slave)
	return nil
}
func (*DictionaryService) DeleteDictionarySlave(SlaveId uint) error {
	slave := new(model.DictionarySlave)
	err := initialization.DB.Model(&model.DictionarySlave{}).Where("id=?", SlaveId).First(slave).Error
	if err != nil {
		return err
	}
	err = initialization.DB.Delete(&model.DictionarySlave{}, SlaveId).Error
	if err != nil {
		return err
	}
	global.DeleteDictionarySlave(slave.DictionaryId, slave.DictionaryId)
	return nil
}
func (*DictionaryService) BatchDeleteDictionarySlave(SlaveId ...uint) error {
	for _, v := range SlaveId {
		slave := new(model.DictionarySlave)
		err := initialization.DB.Model(&model.DictionarySlave{}).Where("id=?", v).First(slave).Error
		if err != nil {
			return err
		}
		err = initialization.DB.Delete(&model.DictionarySlave{}, v).Error
		if err != nil {
			return err
		}
		global.DeleteDictionarySlave(slave.DictionaryId, slave.DictionaryId)
	}
	return nil
}
func (*DictionaryService) UpdateDictionarySlave(slave model.DictionarySlave) error {
	err := initialization.DB.Updates(&slave).Select("dictionary_id").Scan(&slave).Error
	if err != nil {
		return err
	}
	global.UpdateDictionarySlave(slave)
	return nil
}
func (*DictionaryService) QueryDictionarySlave(page, pageSize, dictionaryId int) (dto.QuerySlaveReturnDto, error) {

	dic := make([]model.DictionarySlave, 0)
	//总数
	var record int64
	err := initialization.DB.Model(&model.DictionarySlave{}).Count(&record).Error
	if err != nil {
		return dto.QuerySlaveReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)

	err = initialization.DB.Model(&model.DictionarySlave{}).Where("dictionary_id=?", dictionaryId).Offset((page) - 1*pageSize).Limit(pageSize).Find(&dic).Error
	if err != nil {
		return dto.QuerySlaveReturnDto{}, err
	}

	return dto.QuerySlaveReturnDto{
		Record: record,
		Pages:  pages,
		Struct: dic,
	}, nil
}
func (*DictionaryService) QueryDictionarySlaveByType(condition string, page, pageSize, dictionaryId int) (dto.QuerySlaveReturnDto, error) {
	dic := make([]model.DictionarySlave, 0)
	//总数
	var record int64
	err := initialization.DB.Model(&model.DictionarySlave{}).Where("dictionary_id=? AND name like ?", dictionaryId, condition).Count(&record).Error
	if err != nil {
		return dto.QuerySlaveReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)

	err = initialization.DB.Model(&model.DictionarySlave{}).Where("dictionary_id=? AND name like ?", dictionaryId, "%"+condition+"%").Offset((page) - 1*pageSize).Limit(pageSize).Find(&dic).Error
	if err != nil {
		return dto.QuerySlaveReturnDto{}, err
	}
	return dto.QuerySlaveReturnDto{
		Record: record,
		Pages:  pages,
		Struct: dic,
	}, nil
}

func QueryDictionaryCodeById(code string) (uint, error) {
	var dic model.Dictionary
	err := initialization.DB.Model(&model.Dictionary{}).Where("code=?", code).First(&dic).Error
	if err != nil {
		return 0, err
	}
	return dic.ID, err
}
